<template>
	<view>
		<view class="uni-container">
			<uni-table>
				<uni-thead>
					<uni-tr>
						<uni-td>
							z
						</uni-td>
						<uni-td>
							<textarea v-model="z" placeholder="请填入1000个z" maxlength="100000">

							</textarea>
						</uni-td>
						<uni-td>
							rdc
						</uni-td>
						<uni-td>
							<textarea v-model="rdc" placeholder="请填入1000个rdc" maxlength="100000">

							</textarea>
						</uni-td>
					</uni-tr>
				</uni-thead>
				<uni-tr>
					<uni-td>
						后z
					</uni-td>
					<uni-td>
						<textarea v-model="new_z" maxlength="100000">

						</textarea>
					</uni-td>
					<uni-td>
						后rdc
					</uni-td>
					<uni-td>
						<textarea v-model="new_rdc" maxlength="100000">

						</textarea>
					</uni-td>
				</uni-tr>
				<uni-tr>
					<view @click="calc">
						计算
					</view>
					<view @click="cmk">
						cmk
					</view>
				</uni-tr>
				<uni-tr>
					<uni-td colspan="6">
						杨工，主要内容要求如下：
						<br />3 采集完1000 PCS后自动锁机，同时通过接口程序通知设参系统，设参系统收到通知信息后启动计算程序，在数据库提取该批产品1000 PCS的测试数据，按要求排序，取相应位置的电性能数据（Q1,Q2,Q3,Q4）等，并按公式计算各PAT上下值。
						<br />動態 PAT 限值 = 穩健平均值 ± 3*稳态西格玛，
						<br />稳健均值=Q2
						<br />稳健西格玛=（Q3-Q1）/1.35
						<br />实际上，收集1000粒合格数据，从小到大排序后，
						<br />稳健均值=Q2=（序号500数值+序号501数值）/2
						<br />稳健西格玛=（Q3-Q1）/1.35=（序号750数值-序号250数值）/1.35
						<br />PAT上限= 穩健平均值 + 3*稳态西格玛
						<br />PAT下限= 穩健平均值 - 3*稳态西格玛
						<br />4 计算得到的上下限值与开始的生产工艺参数进行对比，下限取大值，上限取小值重新设置到电桥上，发送信息通知IOT系统，IOT系统收到信息后解除锁机，员工重新开机生产。
					</uni-td>
				</uni-tr>
				<uni-tr>
					<uni-td colspan="6">
						<textarea v-model="sortArrStr" maxlength="100000">
						</textarea>
					</uni-td>
				</uni-tr>
				<uni-tr>
					<uni-td>
						Q1</uni-td>
					<uni-td><input v-model="q1" type="number" placeholder="Q1" />
					</uni-td>
					<uni-td>
						Q3</uni-td>
					<uni-td><input v-model="q3" type="number" placeholder="Q3" />
					</uni-td>
					<uni-td>
						Q2</uni-td>
					<uni-td><input v-model="q2" type="number" placeholder="Q2" />
					</uni-td>

				</uni-tr>
				<uni-tr>
					<uni-td>
						sigma</uni-td>
					<uni-td><input v-model="sigma" type="number" placeholder="sigma" />
					</uni-td>
					<uni-td>
						PAT参数（上限）</uni-td>
					<uni-td><input v-model="pat_hight" type="number" placeholder="pat_hight" />
					</uni-td>
					<uni-td>
						PAT参数（下限）</uni-td>
					<uni-td><input v-model="pat_low" type="number" placeholder="pat_low" />
					</uni-td>
				</uni-tr>
				<uni-tr>
					<uni-td colspan="6">
						<view @click="patCalc(0,3)">
							pat计算Z(3 sigma)
						</view>
						<view @click="patCalc(1,3)">
							pat计算RDC(3 sigma)
						</view>
						<view @click="patCalc(0,6)">
							pat计算Z(6 sigma)
						</view>
						<view @click="patCalc(1,6)">
							pat计算RDC(6 sigma)
						</view>
						<view @click="patCalc(0,1)">
							经典pat计算Z( sigma)
						</view>
						<view @click="patCalc(1,1)">
							经典pat计算RDC( sigma)
						</view>
					</uni-td>
				</uni-tr>
			</uni-table>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				z: '',
				rdc: '',
				new_z: '',
				new_rdc: '',
				q1: '',
				q3: '',
				q2: '',
				sigma: '',
				pat_hight: '',
				pat_low: '',
				sortArr: '',
				sortArrStr: '',
				okRate: '', //合格率
			}
		},
		methods: {
			calc() {
				var zArr = [];
				zArr = this.z.split(",");
				this.new_z = '';
				for (var i = 0; i < zArr.length; i++) {
					this.new_z += zArr[i] + '\n';
				}

				var rdcArr = [];
				rdcArr = this.rdc.split(",");
				this.new_rdc = '';
				for (var i = 0; i < rdcArr.length; i++) {
					this.new_rdc += rdcArr[i] + '\n';
				}
			},
			patCalc(type, six_three) {
				var rate = six_three;
				var Arr = [];
				//z
				if (type == 0) {
					Arr = this.z.split(",");
					//rdc	
				} else if (type == 1) {
					Arr = this.rdc.split(",");
				}
				var sortArr = Arr.sort((a, b) => a - b);
				var q1 = parseFloat(sortArr[249]);
				var q2 = (parseFloat(sortArr[499]) + parseFloat(sortArr[500])) / 2;
				var q3 = parseFloat(sortArr[749]);
				var sigma = (q3 - q1) / 1.35;
				//经典计算方式
				if (six_three == 1) {
					var avg=this.average(sortArr);
					console.log('avg'+avg);
					var pat_hight = avg + 3 * sigma;
					var pat_low = avg - 3 * sigma;
				} else {
					var pat_hight = q2 + rate * sigma;
					var pat_low = q2 - rate * sigma;
				}


				//key: "value",
				this.q1 = q1;
				this.q2 = q2;
				this.q3 = q3;
				this.sigma = sigma;
				this.pat_hight = pat_hight;
				this.pat_low = pat_low;
				this.sortArrStr = '';
				this.sortArrStr += (type == 0 ? 'z':'rdc') + (six_three==1?'经典':six_three) + 'sigma\n';
				this.sortArrStr += '合格率:' + this.calcOkRate(sortArr, pat_hight, pat_low) + '\n';
				console.log(this.sortArrStr);
				for (var i = 0; i < sortArr.length; i++) {
					this.sortArrStr += i + ':' + sortArr[i] + '\n';
				}

			},
			calcOkRate(Arr, hight, low) {
				var sum = 0;
				for (var i = 0; i < Arr.length; i++) {
					var item = Arr[i];
					if (item >= low && item <= hight) {
						sum++;
					}
				}
				// console.log('calcOkRate:' + sum);
				return sum / 1000;
			},
			//计算数组平均值
			average(numbers) {
				return (numbers.length) ? (numbers.reduce((total,num) => total +parseFloat(num), 0) /numbers
					.length) : 0;
			},
			cmk(){
				var sum=0;
				var zArr = [];
				zArr = this.z.split(",");
				var newz='';
				for (var i = 0; i < zArr.length; i++) {
					if(sum<50){
						if(parseFloat(zArr[i])>116.4&&parseFloat(zArr[i])<123.6){
							newz += zArr[i] + '\n';
							sum++;
						}
					}
					
					
				}
				console.log(newz);
			}
		}
	}
</script>

<style>

</style>